<?php

//No direct acesss
defined('_JEXEC') or die();
jimport('joomla.application.component.model');

class SimplelistingModelListings extends JModel {
        var $_data = null;
	var $_total = null;
	var $_pagination = null;
	var $_table_prefix = null;


    function __construct()
    {
        parent::__construct();
        global $mainframe, $context;

        $this->_table_prefix = '#__simplelisting';

        $params =&$mainframe->getParams();

        $limit	= $mainframe->getUserStateFromRequest( $context.'limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
        $limitstart = JRequest::getVar('limitstart', 0, '', 'int');
        $limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);

        $this->setState('limit', $limit);
        $this->setState('limitstart', $limitstart);
    }

    function getData()
    {
        if (empty($this->_data))
        {
            $query = $this->_buildQuery();
            $this->_data = $this->_getList($query, $this->getState('limitstart'), $this->getState('limit'));
        }
        return $this->_data;
    }


    function getTotal()
    {
        if (empty($this->_total))
        {
            $query = $this->_buildQuery();
            $this->_total = $this->_getListCount($query);
        }
        return $this->_total;
    }


    function getPagination()
    {
        if (empty($this->_pagination)) {
            jimport('joomla.html.pagination');
            $this->_pagination = new JPagination($this->getTotal(), $this->getState('limitstart'), $this->getState('limit') );
        }
        return $this->_pagination;
    }

    function _buildQuery()
    {
        $where = $this->_buildContentWhere();
        $orderby = $this->_buildContentOrderBy();

        $query = 'SELECT a.*,'.
        ' u.name as `username`, u.email as `email`, u.id as `userid`,'.
        ' t.name as `type`, ca.name as `category`, l.name as `layout`,co.name as `country`,et.name as `extra`,'.
        ' cd.telephone as `tel`, cd.mobile `mobile`, cd.fax `fax`, cd.country as `usercountry`'.
        ' FROM '.$this->_table_prefix.' AS a' .
        ' LEFT JOIN '.$this->_table_prefix.'_types AS t ON t.id = a.type_id '.
        ' LEFT JOIN '.$this->_table_prefix.'_categories AS ca ON ca.id = a.category_id'.
        ' LEFT JOIN '.$this->_table_prefix.'_layouts AS l ON l.id = a.layout_id'.
        ' LEFT JOIN '.$this->_table_prefix.'_countries AS co ON co.id = a.countries_id'.
        ' LEFT JOIN '.$this->_table_prefix.'_extras AS et ON et.id = a.extra_id'.
        ' LEFT JOIN #__users AS u ON u.id = a.create_by' .
        ' LEFT JOIN #__contact_details AS cd ON cd.user_id = a.create_by' .
        $where
        .$orderby;
        return $query;
    }

    function _buildContentWhere()
    {    
        global $mainframe;
        $params =& $mainframe->getParams();
        $user =& JFactory::getuser();
        $_catid =& JRequest::getVar('catid', 0);
        $_typeid =& JRequest::getVar('typeid', 0);
        $_extraid =& JRequest::getVar('extraid', 0);
        $_countryid =& JRequest::getVar('countryid', 0);
        $_layoutid =& JRequest::getVar('layoutid', 0);

        //All Listings
        if (($_catid != 0) and ($_typeid != 0) and ($_extraid != 0) and ($_countryid != 0) and ($_layoutid == 0)){
            $where= ' WHERE a.published=1 AND t.published=1 AND ca.published=1 '.
                    ' AND a.category_id='. $_catid.' AND a.type_id='. $_typeid.' AND a.extra_id='. $_extraid.' AND a.countries_id='. $_countryid.' AND a.layout_id='. $_layoutid.'';
        //Category only
        } elseif (($_catid != 0) and ($_typeid == 0) and ($_extraid == 0) and ($_countryid == 0) and ($_layoutid == 0)) {
            $where= ' WHERE a.published=1 AND t.published=1 AND ca.published=1 '.
                    ' AND t.published AND a.category_id='. $_catid.'';
        //Type only
        } elseif (($_typeid != 0) and ($_catid == 0) and ($_extraid == 0) and ($_countryid == 0) and ($_layoutid == 0)) {
            $where= ' WHERE a.published=1 AND t.published=1 AND ca.published=1 '.
                    ' AND t.published AND a.type_id='. $_typeid.'';
        //Extra Only
        } elseif (($_typeid == 0) and ($_catid == 0) and ($_extraid != 0) and ($_countryid == 0) and ($_layoutid == 0)) {
            $where= ' WHERE a.published=1 AND t.published=1 AND ca.published=1 '.
                    ' AND a.extra_id='. $_extraid.'';
        //Country Only
        } elseif (($_typeid == 0) and ($_catid == 0) and ($_extraid == 0) and ($_countryid != 0) and ($_layoutid == 0)) {
            $where= ' WHERE a.published=1 AND t.published=1 AND ca.published=1 '.
                    ' AND a.countries_id='. $_countryid.'';
        //Layout Only
        } elseif (($_typeid == 0) and ($_catid == 0) and ($_extraid == 0) and ($_countryid == 0) and ($_layoutid != 0)) {
            $where= ' WHERE a.published=1 AND t.published=1 AND ca.published=1 '.
                    ' AND a.layout_id='. $_layoutid.'';
        //Category and Type
        } elseif (($_typeid != 0) and ($_catid != 0) and ($_extraid == 0) and ($_countryid == 0) and ($_layoutid == 0)) {
            $where= ' WHERE a.published=1 AND t.published=1 AND ca.published=1 '.
                    ' AND a.category_id='. $_catid.' AND a.type_id='. $_typeid.'';
        //Category and Extra
        } elseif (($_typeid == 0) and ($_catid != 0) and ($_extraid != 0) and ($_countryid == 0) and ($_layoutid == 0)) {
            $where= ' WHERE a.published=1 AND t.published=1 AND ca.published=1 '.
                    ' AND a.category_id='. $_catid.' AND a.extra_id='. $_extraid.'';
        //Category and Country
        } elseif (($_typeid == 0) and ($_catid != 0) and ($_extraid == 0) and ($_countryid != 0) and ($_layoutid == 0)) {
            $where= ' WHERE a.published=1 AND t.published=1 AND ca.published=1 '.
                    ' AND a.category_id='. $_catid.' AND a.countries_id='. $_countryid.'';
        //Category and Layout
        } elseif (($_typeid == 0) and ($_catid != 0) and ($_extraid == 0) and ($_countryid == 0)and ($_layoutid != 0)) {
            $where= ' WHERE a.published=1 AND t.published=1 AND ca.published=1 '.
                    ' AND a.category_id='. $_catid.' AND a.layout_id='. $_layoutid.'';
        //Type and Extra
        } elseif (($_typeid != 0) and ($_catid == 0) and ($_extraid != 0) and ($_countryid == 0) and ($_layoutid == 0)) {
            $where= ' WHERE a.published=1 AND t.published=1 AND ca.published=1 '.
                    ' AND a.type_id='. $_typeid.' AND a.extra_id='. $_extraid.'';
        //Type and Country
        } elseif (($_typeid != 0) and ($_catid == 0) and ($_extraid == 0) and ($_countryid != 0) and ($_layoutid == 0)) {
            $where= ' WHERE a.published=1 AND t.published=1 AND ca.published=1 '.
                    ' AND a.type_id='. $_typeid.' AND a.countries_id='. $_countryid.'';
        //Type and Layout
        } elseif (($_typeid != 0) and ($_catid == 0) and ($_extraid == 0) and ($_countryid == 0) and ($_layoutid != 0)) {
            $where= ' WHERE a.published=1 AND t.published=1 AND ca.published=1 '.
                    ' AND a.type_id='. $_typeid.' AND a.layout_id='. $_layoutid.'';
        //Extra and Country
        } elseif (($_typeid == 0) and ($_catid == 0) and ($_extraid != 0) and ($_countryid != 0) and ($_layoutid == 0)) {
            $where= ' WHERE a.published=1 AND t.published=1 AND ca.published=1 '.
                    ' AND a.extra_id='. $_extraid.' AND a.countries_id='. $_countryid.'';
        //Extra and Layout
        } elseif (($_typeid == 0) and ($_catid == 0) and ($_extraid != 0) and ($_countryid == 0) and ($_layoutid != 0)) {
            $where= ' WHERE a.published=1 AND t.published=1 AND ca.published=1 '.
                    ' AND a.extra_id='. $_extraid.' AND a.layout_id='. $_layoutid.'';
        //Country and Layout
        } elseif (($_typeid == 0) and ($_catid == 0) and ($_extraid == 0) and ($_countryid != 0) and ($_layoutid != 0)) {
            $where= ' WHERE a.published=1 AND t.published=1 AND ca.published=1 '.
                    ' AND a.countries_id='. $_countryid.' AND a.layout_id='. $_layoutid.'';
        //Category, Type and Extra
        } elseif (($_typeid != 0) and ($_catid != 0) and ($_extraid != 0) and ($_countryid == 0) and ($_layoutid == 0)) {
            $where= ' WHERE a.published=1 AND t.published=1 AND ca.published=1 '.
                    ' AND a.category_id='. $_catid.' AND a.type_id='. $_typeid.' AND a.extra_id='. $_extraid.'';
        //Category, Type and Country
        } elseif (($_typeid != 0) and ($_catid != 0) and ($_extraid == 0) and ($_countryid != 0) and ($_layoutid == 0)) {
            $where= ' WHERE a.published=1 AND t.published=1 AND ca.published=1 '.
                    ' AND a.category_id='. $_catid.' AND a.type_id='. $_typeid.' AND a.countries_id='. $_countryid.'';
        //Category, Type and Layout
        } elseif (($_typeid != 0) and ($_catid != 0) and ($_extraid == 0) and ($_countryid == 0) and ($_layoutid != 0)) {
            $where= ' WHERE a.published=1 AND t.published=1 AND ca.published=1 '.
                    ' AND a.category_id='. $_catid.' AND a.type_id='. $_typeid.' AND a.layout_id='. $_layoutid.'';
        //Category, Extra and Country
        } elseif (($_typeid == 0) and ($_catid != 0) and ($_extraid != 0) and ($_countryid != 0) and ($_layoutid == 0)) {
            $where= ' WHERE a.published=1 AND t.published=1 AND ca.published=1 '.
                    ' AND a.category_id='. $_catid.' AND a.extra_id='. $_extraid.' AND a.countries_id='. $_countryid.'';
        //Category, Layout and Country
        } elseif (($_typeid == 0) and ($_catid != 0) and ($_extraid == 0) and ($_countryid != 0) and ($_layoutid != 0)) {
            $where= ' WHERE a.published=1 AND t.published=1 AND ca.published=1 '.
                    ' AND a.category_id='. $_catid.' AND a.layout_id='. $_layoutid.' AND a.countries_id='. $_countryid.'';
        //Type, Extra and Country
        } elseif (($_typeid != 0) and ($_catid == 0) and ($_extraid != 0) and ($_countryid != 0) and ($_layoutid == 0)) {
            $where= ' WHERE a.published=1 AND t.published=1 AND ca.published=1 '.
                    ' AND a.type_id='. $_typeid.' AND a.extra_id='. $_extraid.' AND a.countries_id='. $_countryid.'';
        //Type, Extra and Layout
        } elseif (($_typeid != 0) and ($_catid == 0) and ($_extraid != 0) and ($_countryid == 0) and ($_layoutid != 0)) {
            $where= ' WHERE a.published=1 AND t.published=1 AND ca.published=1 '.
                    ' AND a.type_id='. $_typeid.' AND a.extra_id='. $_extraid.' AND a.layout_id='. $_layoutid.'';
        //Category, Type, Layout and Country
        } elseif (($_typeid != 0) and ($_catid != 0) and ($_extraid == 0) and ($_countryid != 0) and ($_layoutid != 0)) {
            $where= ' WHERE a.published=1 AND t.published=1 AND ca.published=1 '.
                    ' AND a.category_id='. $_catid.' AND a.type_id='. $_typeid.' AND a.layout_id='. $_layoutid.' AND a.countries_id='. $_countryid.'';
        //Category, Type, Extra and Layout
        } elseif (($_typeid != 0) and ($_catid != 0) and ($_extraid != 0) and ($_countryid == 0) and ($_layoutid != 0)) {
            $where= ' WHERE a.published=1 AND t.published=1 AND ca.published=1 '.
                    ' AND a.category_id='. $_catid.' AND a.type_id='. $_typeid.' AND a.extra_id='. $_extraid.' AND a.layout_id='. $_layoutid.'';
        //Category, Type, Extra and Country
        } elseif (($_typeid != 0) and ($_catid != 0) and ($_extraid != 0) and ($_countryid != 0) and ($_layoutid == 0)) {
            $where= ' WHERE a.published=1 AND t.published=1 AND ca.published=1 '.
                    ' AND a.category_id='. $_catid.' AND a.type_id='. $_typeid.' AND a.extra_id='. $_extraid.' AND a.countries_id='. $_countryid.'';
        //Category, Extra, Country and Layout
        } elseif (($_typeid == 0) and ($_catid != 0) and ($_extraid != 0) and ($_countryid != 0) and ($_layoutid != 0)) {
            $where= ' WHERE a.published=1 AND t.published=1 AND ca.published=1 '.
                    ' AND a.category_id='. $_catid.' AND a.extra_id='. $_extraid.' AND a.countries_id='. $_countryid.' AND a.layout_id='. $_layoutid.'';
        //Type, Extra, Country and Layout
        } elseif (($_typeid != 0) and ($_catid == 0) and ($_extraid != 0) and ($_countryid != 0) and ($_layoutid != 0)) {
            $where= ' WHERE a.published=1 AND t.published=1 AND ca.published=1 '.
                    ' AND a.type_id='. $_typeid.' AND a.extra_id='. $_extraid.' AND a.countries_id='. $_countryid.' AND a.layout_id='. $_layoutid.'';

        } else {
            $where= ' WHERE a.published=1 AND t.published=1 AND ca.published=1';
        }
        return $where;
    }

    function _buildContentOrderBy()
    {
        global $mainframe;
        $params =& $mainframe->getParams();
        $orderby =& JRequest::getVar('orderby', '');

        if ($orderby == 'order'){
                $orderby = ' ORDER BY a.ordering ASC ';

        } elseif ($orderby == 'date'){
                $orderby = ' ORDER BY a.date_created ASC ';

        } elseif ($orderby == 'rdate'){
                $orderby = ' ORDER BY a.date_created DESC ';

        } elseif ($orderby == 'alpha'){
                $orderby = ' ORDER BY a.name ASC ';

        } elseif ($orderby == 'ralpha'){
                $orderby = ' ORDER BY a.name DESC ';

        } elseif ($orderby == 'price'){
                $orderby = ' ORDER BY a.price DESC ';

        } elseif ($orderby == 'rprice'){
                $orderby = ' ORDER BY a.price ASC ';

        } else {
                $orderby = ' ORDER BY a.date_created DESC';
        }
        return $orderby;
    }

    /**
    * Method to override the default limit configuration
    *
    * @access public
    * @param integer
    * @return boolean
    */
    public function setLimit( $limit ) {
        if( $limit > 0 ) {
            $this->setState('limit', $limit);
            return true ;
        }
        return false ;
    }
}
?>
